/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;


import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.KeyNamePairs;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ConditionPanel extends Content {

	private TextBox name, code, mappingCode;
	private ListBox status, app, remote;
	private ServiceMessages cons;
	private GwtContext context;
	private SqlListener listener;
	private String appId, domainId;

	public ConditionPanel(ServiceMessages scons, GwtContext context,
			SqlListener slistener, String aid, String did) {
		this.cons = scons;
		this.context = context;
		this.listener = slistener;
		this.appId = aid;
		this.domainId = did;
		VerticalPanel panel = new VerticalPanel();
		//panel.setBorderWidth(1);
		panel.setWidth("100%");
		panel.add(new HTML(cons.fillSearchCondition()));

		Button search = new Button(cons.search());

		FlexTable table = new FlexTable();
		
		table.setWidth("100%");
		
		ColumnFormatter cc = table.getColumnFormatter();
		cc.setWidth(0, "15%");
		cc.setWidth(1, "20%");
		cc.setWidth(2, "15%");
		cc.setWidth(3, "20%");
		cc.setWidth(4, "15%");
		cc.setWidth(5, "15%");
		
		
		FlexCellFormatter cf = table.getFlexCellFormatter();
		
		table.setCellSpacing(1);
		table.setCellPadding(0);
		table.setBorderWidth(1);

		table.setHTML(0, 0, cons.name());
		cf.setStyleName(0, 0, "tablecol");
		
		name = new TextBox();
		name.setVisibleLength(20);
		table.setWidget(0, 1, name);

		table.setHTML(0, 2, cons.code());
		cf.setStyleName(0, 2, "tablecol");

		code = new TextBox();
		code.setVisibleLength(20);
		table.setWidget(0, 3, code);

		

		table.insertRow(1);

		if (StringUtils.isEmpty(appId) && StringUtils.isEmpty(domainId)) {
			table.setHTML(0, 4, cons.status());
			cf.setStyleName(0, 4, "tablecol");
			status = new ListBox();
			status.addItem(" ", "");
			status.addItem(cons.published(),
					EntityConstants.SERVICE_STATUS_PUBLISHED);
			status.addItem(cons.unpublished(),
					EntityConstants.SERVICE_STATUS_UNPUBLISHED);
			table.setWidget(0, 5, status);
			
			table.setHTML(1, 0, cons.localApp());
			cf.setStyleName(1, 0, "tablecol");
			app = new ListBox();
			app.addItem(" ", "");

			ArrayList apps = (ArrayList) context.get(ServiceConstants.APP_LIST);
			if (apps != null && apps.size() > 0) {
				for (int i = 0, j = apps.size(); i < j; i++) {
					KeyNamePairs k = (KeyNamePairs) apps.get(i);
					app.addItem(k.getName(), k.getId());
				}
			}
			table.setWidget(1, 1, app);

			table.setHTML(1, 2, cons.remoteDomain1());
			cf.setStyleName(1, 2, "tablecol");
			remote = new ListBox();
			remote.addItem(" ", "");
			apps = (ArrayList) context.get(ServiceConstants.DOMAIN_LIST);
			if (apps != null && apps.size() > 0) {
				for (int i = 0, j = apps.size(); i < j; i++) {
					KeyNamePairs k = (KeyNamePairs) apps.get(i);
					remote.addItem(k.getName(), k.getId());
				}
			}
			table.setWidget(1, 3, remote);
			table.setWidget(1, 4, search);
			cf.setColSpan(1, 4, 2);

		} else if (StringUtils.isEmpty(appId) && !StringUtils.isEmpty(domainId)) {
			table.setHTML(0, 2, cons.localCode());
			
			table.setHTML(0, 4, cons.domain());
			cf.setStyleName(0, 4, "tablecol");
			remote = new ListBox();
			// remote.addItem("&nbsp;&nbsp;", "");
			ArrayList apps = (ArrayList) context
					.get(ServiceConstants.DOMAIN_LIST);
			if (apps != null && apps.size() > 0) {
				for (int i = 0, j = apps.size(); i < j; i++) {
					KeyNamePairs k = (KeyNamePairs) apps.get(i);

					if (k.getId().equals(domainId)) {
						remote.addItem(k.getName(), k.getId());
						remote.setItemSelected(0, true);
						break;
					}
				}
			}
			// remote.setEnabled(false);
			table.setWidget(0, 5, remote);
			table.setHTML(1, 0, cons.remoteCode());
			cf.setStyleName(1, 0, "tablecol");
			mappingCode = new TextBox();
			mappingCode.setVisibleLength(20);
			table.setWidget(1, 1, mappingCode);
			table.setWidget(1, 2, search);
			cf.setColSpan(1, 2, 4);
		} else if (!StringUtils.isEmpty(appId) && StringUtils.isEmpty(domainId)) {
			table.setHTML(0, 4, cons.status());
			cf.setStyleName(0, 4, "tablecol");
			status = new ListBox();
			status.addItem(" ", "");
			status.addItem(cons.published(),
					EntityConstants.SERVICE_STATUS_PUBLISHED);
			status.addItem(cons.unpublished(),
					EntityConstants.SERVICE_STATUS_UNPUBLISHED);
			table.setWidget(0, 5, status);
			
			table.setHTML(1, 0, cons.application());
			cf.setStyleName(1, 0, "tablecol");
			app = new ListBox();
			// app.addItem("&nbsp;&nbsp;", "");

			ArrayList apps = (ArrayList) context.get(ServiceConstants.APP_LIST);
			if (apps != null && apps.size() > 0) {
				for (int i = 0, j = apps.size(); i < j; i++) {
					KeyNamePairs k = (KeyNamePairs) apps.get(i);

					if (k.getId().equals(appId)) {
						app.addItem(k.getName(), k.getId());
						app.setItemSelected(0, true);
						break;
					}
				}
			}

			// app.setEnabled(false);
			table.setWidget(1, 1, app);
			table.setWidget(1, 2, search);
			cf.setColSpan(1, 2, 4);
		}

		search.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {

				StringBuffer hql = new StringBuffer();
				if (!StringUtils.isEmpty(appId)) {
					int i = app.getSelectedIndex();
					hql.append("from SoaService s where s.appId = '"
							+ app.getValue(i) + "' ");
					if (!StringUtils.isEmpty(name.getText())) {
						hql.append(" and s.serviceName like '%" + name.getText()
								+ "%' ");
					}
					if (!StringUtils.isEmpty(code.getText())) {
						hql.append(" and s.serviceCode like '%" + code.getText()
								+ "%' ");
					}
					if (status.getSelectedIndex() > 0) {
						int s = status.getSelectedIndex();
						hql.append(" and s.status = '" + status.getValue(s)
								+ "' ");
					}
					listener.onServiceSql(hql.toString());
					return;

				} else if (!StringUtils.isEmpty(domainId)) {
					int i = remote.getSelectedIndex();
					hql.append("from SoaMappingService s where s.domainId = '"
							+ remote.getValue(i) + "' ");
					if (!StringUtils.isEmpty(name.getText())) {
						hql.append(" and s.name like '%" + name.getText()
								+ "%' ");
					}
					if (!StringUtils.isEmpty(code.getText())) {
						hql.append(" and s.localCode like '%" + code.getText()
								+ "%' ");
					}
					if (!StringUtils.isEmpty(mappingCode.getText())) {
						hql.append(" and s.remoteCode like '%" + mappingCode.getText()
								+ "%' ");
					}
					listener.onMappingServiceSql( hql.toString());
					return;

				} else {
					if ((app.getSelectedIndex() > 0
							&& remote.getSelectedIndex() > 0) || (app.getSelectedIndex() <= 0
									&& remote.getSelectedIndex() <= 0)) {
						Window.alert(cons.chooseError());
						return;
					}

					if (app.getSelectedIndex() > 0) {
						int i = app.getSelectedIndex();
						hql.append("from SoaService s where s.appId = '"
								+ app.getValue(i) + "' ");
						if (!StringUtils.isEmpty(name.getText())) {
							hql.append(" and s.serviceName like '%" + name.getText()
									+ "%' ");
						}
						if (!StringUtils.isEmpty(code.getText())) {
							hql.append(" and s.serviceCode like '%" + code.getText()
									+ "%' ");
						}
						if (status.getSelectedIndex() > 0) {
							int s = status.getSelectedIndex();
							hql.append(" and s.status = '" + status.getValue(s)
									+ "' ");
						}
						listener.onServiceSql(hql.toString());
						return;

					} else if (remote.getSelectedIndex() > 0) {
						int i = remote.getSelectedIndex();
						hql
								.append("from SoaMappingService s where s.domainId = '"
										+ remote.getValue(i) + "' ");
						if (!StringUtils.isEmpty(name.getText())) {
							hql.append(" and s.serviceName like '%" + name.getText()
									+ "%' ");
						}
						if (!StringUtils.isEmpty(code.getText())) {
							hql.append(" and s.serviceCode like '%" + code.getText()
									+ "%' ");
						}
						listener.onMappingServiceSql( hql.toString());
						return;
					}
				}
			}

		});
		
		panel.add(table);
		initWidget(panel);

	}

}
